package com.hxh.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.hxh.entity.po.ClassInfo;
import com.hxh.entity.po.FieldName;
import com.hxh.util.GenUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 项目-页面配置
 **/
@Data
public class ProjectTablePageConfig implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 自增id
     * 索引:PRI
     **/
    private Integer id;

    /**
     * 项目id
     * 索引:MUL
     **/
    private Integer projectId;

    /**
     * 数据源id
     */
    private Integer datasourceId;
    private ProjectDatasource projectDatasource;

    /**
     * 表名字
     * 索引:MUL
     **/
    private String tableName;

    /**
     * 搜索条件字段
     **/
    private String searchFields;

    /**
     * 展示表字段
     **/
    private String tableColumnFields;

    /**
     * 可新增字段
     **/
    private String addFields;

    /**
     * 可修改字段
     **/
    private String updateFields;

    /**
     * 是否显示增加按钮： ：0否 1是
     **/
    private String showAdd;

    /**
     * 是否显示批量增加按钮： ：0否 1是
     **/
    private String showBatchAdd;

    /**
     * 是否显示批量导出按钮： ：0否 1是
     **/
    private String showBatchExport;

    /**
     * 是否显示删除按钮： ：0否 1是
     **/
    private String showDelete;

    /**
     * 是否显示修改按钮： ：0否 1是
     **/
    private String showUpdate;

    /**
     * 创建时间
     **/
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private java.util.Date createTime;

    /**
     * 更新时间
     **/
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private java.util.Date updateTime;


    public static ProjectTablePageConfig generationDefaultByClassInfo(ClassInfo classInfo) {
        ProjectTablePageConfig projectTablePageConfig = new ProjectTablePageConfig();
        projectTablePageConfig.setTableName(classInfo.getTableName());
        List<String> tableNameListWithTime = classInfo.getFieldNameList().stream().map(FieldName::getName).collect(Collectors.toList());
        String allFieldsWithTime = StringUtils.join(tableNameListWithTime, ",");
        projectTablePageConfig.setTableColumnFields(allFieldsWithTime);

        List<String> tableNameList = tableNameListWithTime.stream().filter(fieldName -> {
            if (GenUtil.AutoProcessCreateTimeFields.equals(fieldName)) {
                return false;
            }
            return !GenUtil.AutoProcessUpdateTimeFields.equals(fieldName);
        }).collect(Collectors.toList());
        String allFields = StringUtils.join(tableNameList, ",");

        projectTablePageConfig.setAddFields(allFields);
        projectTablePageConfig.setSearchFields(allFields);
        projectTablePageConfig.setUpdateFields(allFields);
        projectTablePageConfig.setShowUpdate("1");
        projectTablePageConfig.setShowDelete("1");
        projectTablePageConfig.setShowAdd("1");
        projectTablePageConfig.setShowBatchAdd("0");
        projectTablePageConfig.setShowBatchExport("0");
        return projectTablePageConfig;
    }
}
